﻿/*
 * 体温表的数据操作
 * */
using System;
using System.Collections.Generic;
using System.Text;
using WH.NurseSys.Model;
using System.Data.SqlClient;
using System.Data;

namespace WH.NurseSys.DAL
{
    public class TemperatureDAL
    {
        //插入一条体温数据
        public bool Insert(Temperature temperature)
        {
            string sqlText = "insert into temperatures (id,patient_id,value,exec_time) values (@id,@patient_id,@value,@exec_time)";
            SqlParameter[] parameters = new SqlParameter[]{
                new SqlParameter("@id",temperature.Id),
                new SqlParameter("@patient_id",temperature.PatientId),
                new SqlParameter("@value",temperature.Value),
                new SqlParameter("@exec_time",temperature.ExecTime)
            };
            int rows = SqlHelper.ExecuteNoQuery(sqlText, parameters);
            if (rows <= 0)
                return false;
            else
                return true;
        }

        //更新一条体温数据
        public bool Update(Temperature temperature)
        {
            string sqlText = "update temperatures set patient_id=@patient_id,value=@value,exec_time=@exec_time where id=@id";
            SqlParameter[] parameters = new SqlParameter[]{
                new SqlParameter("@id",temperature.Id),
                new SqlParameter("@patient_id",temperature.PatientId),
                new SqlParameter("@value",temperature.Value),
                new SqlParameter("@exec_time",temperature.ExecTime)
            };
            int rows = SqlHelper.ExecuteNoQuery(sqlText, parameters);
            if (rows <= 0)
                return false;
            else
                return true;
        }

        //删除一条体温数据
        public bool Delete(Guid id)
        {
            string sqlText = "delete from temperatures where id=@id";
            SqlParameter parameters = new SqlParameter("@id", id);
            int rows = SqlHelper.ExecuteNoQuery(sqlText, parameters);
            if (rows <= 0)
                return false;
            else
                return true;
        }

        //获取一条体温数据
        public Temperature GetById(Guid id)
        {
            string sqlText = "select patient_id,value,exec_time from temperatures where id=@id";
            SqlParameter parameters = new SqlParameter("@id", id);
            DataTable dbTable = SqlHelper.ExecuteTable(sqlText, parameters);
            if (dbTable.Rows.Count <= 0)
                return null;
            DataRow dbRow = dbTable.Rows[0];
            Temperature temperature = new Temperature();
            temperature.Id = id;
            temperature.PatientId = new Guid(dbRow["patient_id"].ToString());
            temperature.Value = Convert.ToSingle(dbRow["value"]);
            temperature.ExecTime = Convert.ToDateTime(dbRow["exec_time"]);
            return temperature;
        }

        //获取一个人的温度数据列表
        public List<Temperature> GetListByPatient(Guid patientId)
        {
            List<Temperature> temperatureList = new List<Temperature>();
            string sqlText = "select * from temperatures where patient_id=@patient_id order by exec_time asc";
            SqlParameter parameter = new SqlParameter("@patient_id", patientId);
            DataTable dbTable = SqlHelper.ExecuteTable(sqlText, parameter);
            foreach (DataRow dbRow in dbTable.Rows)
            {
                Temperature temperature = new Temperature();
                temperature.Id = (Guid)dbRow["id"];
                temperature.ExecTime = Convert.ToDateTime(dbRow["exec_time"]);
                temperature.PatientId = (Guid)dbRow["patient_id"];
                temperature.Value = Convert.ToSingle(dbRow["value"]);
                temperatureList.Add(temperature);
            }
            return temperatureList;
        }
    }
}
